using System.IO;
using System.Text;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace BearUtil
{
    public class CreateNewDialog : CreateNodeBasic<CreateNewDialog>
    {
        public const string MenuName = "创建新对话";

        public override string DefaultPath
        {
            get { return "Assets/StoryEditor/CreateNewDialog/CreateNewDialog.asset"; }
        }

        public override string GeneratePath
        {
            get => "Assets/StoryEditor/Nodes/Dialogs/DialogueNode/";
        }

        public override string FormatPath
        {
            get => "Assets/StoryEditor/Nodes/Format/";
        }
        
        [LabelText("DialogNode属性"), InfoBox("兼容旧数据", InfoMessageType.Warning), TabGroup("旧数据兼容")]
        public List<NodeProperty> OldProperty;
        

        [Button("创建新节点",ButtonSizes.Large), TabGroup("使用")]
        public override void NewNode()
        {
            CtrlAllDynamicNode();

            if (string.IsNullOrEmpty(NodeName) || _nodes.Find(n => n.Name == NodeName))
            {
                Debug.LogError("节点名称不能为空 || 节点重名，重新定义");
                return;
            }

            if (node == null)
            {
                node = ScriptableObject.CreateInstance<DynamicDialogue>();
            }

            var nodePath = GeneratePath + NodeName + ".asset";
            node.Name = NodeName;
            AssetDatabase.CreateAsset(node, nodePath);
            AssetDatabase.Refresh();

            CtrlAllDynamicNode();
        }
        
        [Button("刷新数据集合", ButtonSizes.Large), GUIColor(0.4f, 0.4f, 0.6f)]
        public override void UpdateFormat()
        {
            List<NodeProperty> props = new List<NodeProperty>();
            for (int i = 0; i < _nodes.Count; i++)
            {
                var node = _nodes[i];
                props.Clear();
                    
                foreach (var nodeProperty in node.props)
                {
                    if (props.Find(p => p.PropName == nodeProperty.PropName) == null)
                        props.Add(nodeProperty);
                }
                
                var fileName = $"{node.Name}Format";
                var filePath = NodeMethodUtil.Delete(FormatPath, fileName + ".cs");
                using (var sw = new StreamWriter(File.Open(filePath, FileMode.OpenOrCreate), Encoding.UTF8))
                {
                    var scriptTxt = new StringBuilder();
                    scriptTxt.Append("using System.ComponentModel;\n");
                    scriptTxt.Append($"public class {fileName} : FormatData\n");
                    scriptTxt.Append("{\n");

                    var name = node.Name;
                    if (name.Equals("DialogNode"))
                        name = "dialogue";
                    scriptTxt.Append("\tpublic override string FileName\n\t{");
                    scriptTxt.Append($"\t\tget => \"{name}\";\n");
                    scriptTxt.Append("\t}\n");
                    
                    for (int m = 0; m < StaticProperty.Count; m++)
                    {
                        var p = StaticProperty[m];
                        // scriptTxt.Append($"\t[Description(\"{p.Desc}\")] ");
                        scriptTxt.Append(GetPropLine(p));
                    }
                    
                    // 增加静态不可修改属性，现在仅支持默认 DialogNode
                    if (node.Name.Equals("DialogNode"))
                    {
                        for (int m = 0; m < OldProperty.Count; m++)
                        {
                            var p = OldProperty[m];
                            scriptTxt.Append(GetPropLine(p));
                        }   
                    }
                    
                    // 增加动态属性
                    for (int n = 0; n < props.Count; n++)
                    {
                        var p = props[n];
                        if (StaticProperty.Find(prop => prop.PropName.Equals(p.PropName)) != null)
                            continue;
                        // scriptTxt.Append($"\t[Description(\"{p.Desc}\")] ");
                        scriptTxt.Append(GetPropLine(p));
                    }

                    scriptTxt.Append("}\n");

                    sw.Write(scriptTxt.ToString());
                    sw.Close();
                }
            }

            AssetDatabase.Refresh();
            Debug.Log("【CreateNewDialog】 Format script 生成成功");
        }
    }
}